<?php

namespace App\Recharge\Api;

use App\Models\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Qingchenwangluo extends AbstractApi
{
    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $array = [
            'order_sn' => (string)$bean->getOrderNo(), //订单号
            'business_number' =>(string)$bean->getSupplierNum(), //商户密钥
            'product_id' =>(string)$bean->getGoodsId(), //商品编码
            'account' => (string)$bean->getAccount(),
            'number' => (string)($bean->getRechargeNum() * $bean->getStartCharging()),
            'price' => $bean->getUnitPrice(),
            'callback_url' =>  urlencode($bean->getNotifyUrl()), //异步回调地址
        ];
        ksort($array);
        $array['sign'] = md5(json_encode($array) .$bean->getSecretKey());
        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('status', '');
        $order_sn = $request->input('order_sn', '');
        return $this
            ->apiNotifyData($order_sn, $status)
            ->setExecute(in_array($status, ['1', '2']))
            ->setRechargeSuccess($status == '1')
            ->setRechargeErrorMessage($request->input('mes', ''));
    }

}
